/*
 * Copyright (c) 2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/sys/IAppDriver.h"
#include "alkes/sys/IApplication.h"

namespace alkes {

#define CALC_FPS(frames, delta) \
    ((frames) / ((delta).asMsec<uint32_t>() / 1000.f))

IAppDriver::IAppDriver()
: interval_(1000)
, frames_(0)
, fps_(0.0f)
, last_draw_()
, application_(0)
{
}

IAppDriver::~IAppDriver()
{
}

void IAppDriver::registerApplicaiton(IApplication* app)
{
    application_ = app;
}

void IAppDriver::setOption(AppDriverOption option, bool enable)
{
    if (options_[option] != enable)
    {
        options_.setFlag(option, enable);

        if (AL_ADO_COUNT_FPS == option)
        {
            frames_ = 0;
            last_draw_ = Time::getNow();
        }
    }
}

void IAppDriver::setFpsUpdateInterval(uint16_t msec_interval)
{
    interval_ = msec_interval;
}

float IAppDriver::getFps() const
{
    return options_[AL_POPT_UPDATED_FPS] ? fps_ : CALC_FPS(frames_, Time::getNow() - last_draw_);
}

void IAppDriver::updateFps()
{
    Time now(Time::getNow());
    Time delta(now - last_draw_);

    ++frames_;
    if (interval_ < delta.asMsec<uint32_t>())
    {
        fps_ = CALC_FPS(frames_ , delta);
        last_draw_ = now;
        frames_ = 0;
        options_.setFlag(AL_POPT_UPDATED_FPS);
    }
    else
    {
        options_.setFlag(AL_POPT_UPDATED_FPS, false);
    }
}

NativeWindow IAppDriver::getNativeWindow() const
{
    return application_->getNativeWindow();
}

bool IAppDriver::invokePreInit(int argc, char* argv[])
{
    return onPreInit(argc, argv);
}

bool IAppDriver::invokeInit(IGraphicsDevice* device)
{
    return onInit(device);
}

void IAppDriver::invokeFin()
{
    onFin();
}

void IAppDriver::invokeDraw()
{
    if (options_[AL_ADO_COUNT_FPS])
        updateFps();

    onDraw();
}

void IAppDriver::invokeResize(uint16_t new_width, uint16_t new_height)
{
    onResize(new_width, new_height);
}

void IAppDriver::invokeDirty(const Rect& rect)
{
    onDirty(rect);
}

void IAppDriver::invokePress(KeyEvent key)
{
    onPress(key);
}

void IAppDriver::invokeRelease(KeyEvent key)
{
    onRelease(key);
}

void IAppDriver::invokeDeviceEvent(uint16_t device_no, uint64_t value)
{
    onDeviceEvent(device_no, value);
}

void IAppDriver::invokeMouseMove(uint16_t x, uint16_t y)
{
    onMouseMove(x, y);
}

bool IAppDriver::onPreInit(int argc, char* argv[])
{
    (void)argc;
    (void)argv;
    return true;
}

bool IAppDriver::onInit(IGraphicsDevice* /*device*/)
{
    return true;
}

void IAppDriver::onFin()
{
}

void IAppDriver::onResize(uint16_t new_width, uint16_t new_height)
{
    (void)new_width;
    (void)new_height;
}

void IAppDriver::onDirty(const Rect& rect)
{
    (void)rect;
}

void IAppDriver::onPress(KeyEvent key)
{
    (void)key;
}

void IAppDriver::onRelease(KeyEvent key)
{
    (void)key;
}

void IAppDriver::onDeviceEvent(uint16_t device_no, uint64_t value)
{
    (void)device_no;
    (void)value;
}

void IAppDriver::onMouseMove(uint16_t x, uint16_t y)
{
    (void)x;
    (void)y;
}

}
